package com.linkoog.devtools.code_generator.setting.configurable.ui.template;

import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.intellij.openapi.ui.Messages;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class TemplateRootPanelBuilder implements PanelBuilder<TemplateNodeData> {

    private JPanel myMainPanel;

    private JLabel nameLabel;


    private JTextField nameTextField;
    private JTextArea descriptionTextArea;

    TemplateNodeData currentTemplateNodeData;


    public TemplateRootPanelBuilder() {
        nameTextField.setEditable(false);
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }


    @Override
    public void disappear() {
        if (currentTemplateNodeData != null && isDataModified()) {
            int result = Messages.showYesNoDialog("是否保存修改？", "保存提示", Messages.getQuestionIcon());
            if (result == Messages.YES) {
                applyData();
            }
        }

        nameTextField.setText("");
        descriptionTextArea.setText("");
    }

    @Override
    public void resetUI(TemplateNodeData data) {
        this.currentTemplateNodeData = data;
        resetData();
    }

    public boolean isDataModified() {
        if (currentTemplateNodeData == null) {
            return false;
        }

        if (nameTextField.getText().equals(currentTemplateNodeData.getName()) && descriptionTextArea.getText().equals(currentTemplateNodeData.getDescription())) {
            return false;
        } else {
            return true;
        }

    }

    public void applyData() {
        if (currentTemplateNodeData != null) {
            currentTemplateNodeData.setName(nameTextField.getText());
            currentTemplateNodeData.setDescription(descriptionTextArea.getText());
        }
    }

    public void resetData() {
        if (currentTemplateNodeData != null) {
            nameTextField.setText(currentTemplateNodeData.getName());
            descriptionTextArea.setText(currentTemplateNodeData.getDescription());
        }
    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
        myMainPanel.add(panel1, BorderLayout.NORTH);
        nameLabel = new JLabel();
        nameLabel.setText("模板名称");
        panel1.add(nameLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        nameTextField = new JTextField();
        panel1.add(nameTextField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel2.setBorder(BorderFactory.createTitledBorder(null, "描述", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        descriptionTextArea = new JTextArea();
        panel2.add(descriptionTextArea, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(-1, 100), new Dimension(150, 100), new Dimension(-1, 100), 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
